<template>
  <LoadPage :show="showPage"/>
  <view class="main">
    <view class="content">
      <view class="top">
        <view class="cate-box dispaly-line-center">
          <view class="one-cate dispaly-center p-22">
            <view class="one-cate-item" v-for="(item, index) in cateList" :key="index"
                  @click="clickCateItem(item, index)">
              <view class="cate-bg">
                <image src="/static/icon/j-ac-cate.png" mode="aspectFill" v-if="index == current"/>
                <image src="/static/icon/j-cate.png" mode="aspectFill" v-else/>
                <view class="txt dispaly-align-center">{{ item.name }}</view>
              </view>
            </view>
          </view>
        </view>
        <view class="cate">
          <view class="cate-box">
            <view class="cate-img">
              <image src="/static/image/sort-bg.png" mode="aspectFill"/>
            </view>
            <view class="sort">
              <view class="sort-item dispaly-center" @click="clickSort(item, index)" v-for="(item, index) in sortList" :key="index">
                <view class="s dispaly-center">
                  <view class="s-left">{{ item.name }}</view>
                  <view class="s-right">
                    <image src="/static/image/sort.png" mode="aspectFill"/>
                  </view>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
      <view class="order-list" :style="{marginTop: cateHeight + 'px'}">
        <view class="order-item" v-for="(item, index) in dataList" :key="index" @click="$goUrl(`/subPackage/index/takeOrdersDetail?id=${item.id}`)">
          <view class="user dispaly-just-between">
            <view class="info dispaly-center">
              <view class="avatar ">
                <image
                    :src="item.avatar"
                    mode="aspectFill"/>
                <image class="q" src="/static/icon/q.png" mode="aspectFill"/>
              </view>
              <view class="name">{{item.nickName}}</view>
            </view>
            <view class="user-id">ID{{item.id}}</view>
          </view>
          <view class="name" style="font-weight: 900">{{item.name}}</view>
          <view class="name over-2">{{item.intro}}</view>
          <view class="time-box dispaly-center">
            <view class="time-icon">
              <image src="/static/icon/time.png" mode="aspectFill"/>
            </view>
            <view class="time-txt">{{item.startTime}} 至 {{item.endTime}}</view>
          </view>
<!--          <view class="desc over-2">-->
<!--            {{item.intro}}-->
<!--          </view>-->
          <view class="money-box dispaly-just-between">
            <view class="money-left">{{Number(item.price).toFixed(2)}}</view>
            <view class="money-right">
              <image src="/static/icon/bf.png" mode="aspectFill"/>
            </view>
          </view>
        </view>
        <LoadMore :status="status"/>
      </view>
    </view>
  </view>
</template>

<script setup>
import {getElementHeight} from '@/utils/util'
import {ref, onMounted, reactive} from 'vue';
import {getProductLit, productCate} from '@/api/apiList';
import {deleteObjectEmptyKey} from '@/utils/util'
import {onReachBottom, onPullDownRefresh} from "@dcloudio/uni-app";

const sortList = ref([
  {name: '时间', type: 1},
  {name: '价格', type: 2},
])

const cateList = ref([])
const getCateList = async () => {
  return new Promise(async (resolve, reject) => {
    const res = await productCate({type: 3})
    if (res.code == 200) {
      cateList.value = res.data
      cateList.value.unshift({name: '全部', id: ''})
      resolve('ok')
    }
  })
}

const dataList = ref([])
const queryData = reactive({
  sortName: 'create_time',
  sortValue: 'desc',
  search: '',
  categoryId: '',
  pageSize: 10,
  pageNum: 1,
  type: 2
})
const status = ref('loadmore')
const showPage = ref(true)
const getListData = async () => {
  return new Promise(async (resolve, reject) => {
    status.value = 'loading'
    const newQuery = deleteObjectEmptyKey(queryData)
    const res = await getProductLit(newQuery)
    if (res.code == 200) {
      if (res.rows.length) {
        dataList.value = [...dataList.value, ...res.rows]
        status.value = 'loadmore'
        resolve('ok')
      } else {
        status.value = 'nomore'
        resolve('ok')
      }
    }
  })
}

const init = async () => {
  const queue = [await getCateList(), await getListData()]
  const res = Promise.all(queue)
  showPage.value = false
}

// 点击排序
const clickSort = (item, index) => {
  if (item.type === 1) queryData.sortName = 'create_time'
  if (item.type === 2) queryData.sortName = 'price'
  if (queryData.sortValue === 'desc') {
    queryData.sortValue = 'asc'
  } else {
    queryData.sortValue = 'desc'
  }
  queryData.pageNum = 1
  dataList.value = []
  getListData()
}

const current = ref(0)
const clickCateItem = (item, index) => {
  current.value = index
  queryData.categoryId = item.id
  queryData.pageNum = 1
  dataList.value = []
  getListData()
}
// 获取元素高度
const cateHeight = ref(0)
onMounted(async () => {
  await init()
  const res = await getElementHeight('.top')
  cateHeight.value = res
  console.log(cateHeight.value, 'cateHeight.value')
})


onReachBottom(() => {
  queryData.pageNum++
  getListData()
})

onPullDownRefresh(() => {
  uni.showLoading({
    title: '加载中'
  })
  queryData.categoryId = ''
  current.value = 0
  queryData.pageNum = 1
  dataList.value = []
  getListData()
  uni.hideLoading()
  uni.stopPullDownRefresh()
})
</script>

<style lang="scss" scoped>
$i-height: 68rpx;
$i-width: 692rpx;
.p-22 {
  padding: 22rpx 18rpx 0 18rpx;
}
.top {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 999;
  background: #ffbf0f;
  .one-cate {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    .one-cate-item {
      .cate-bg {
        width: 178rpx;
        height: 82rpx;
        position: relative;

        image {
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          width: 178rpx;
          height: 82rpx;
        }

        .txt {
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          width: 178rpx;
          height: 82rpx;
          font-family: IPix;
          font-size: 30rpx;
          color: #4E3B0C;
        }
      }
    }
  }
}

.content {
  .cate {
    padding: 0 22rpx;

    .cate-box {
      position: relative;

      .cate-img {
        image {
          width: 100%;
          height: 78rpx;
        }
      }

      .sort {
        display: flex;
        justify-content: flex-end;
        align-items: center;
        position: absolute;
        top: 20%;
        right: 40rpx;

        .sort-item:last-child {
          margin-right: 0;
        }

        .sort-item {
          margin-right: 60rpx;

          .s {
            .s-left {
              font-family: IPix;
              font-size: 28rpx;
              color: #4E3B0C;
              margin-right: 10rpx;
            }

            .s-right {
              image {
                width: 22rpx;
                height: 30rpx;
              }
            }
          }
        }
      }
    }
  }
}

.order-list {
  padding: 22rpx;

  .order-item:last-child {
    margin-bottom: 0;
  }

  .order-item {
    margin-bottom: 28rpx;
    border: 6rpx solid #4e3b0c;
    padding: 24rpx;
    background-color: #ffd257;

    .name {
      font-family: IPix;
      font-size: 28rpx;
      color: #000000;
      margin: 30rpx 0 15rpx 0;
    }

    .user {
      .info {
        .avatar {
          margin-right: 20rpx;
          border: 1rpx solid #FFFFFF;
          border-radius: 50%;
          width: 74rpx;
          height: 74rpx;
          position: relative;

          .q {
            width: 74rpx;
            height: 74rpx;
          }

          image {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            width: 74rpx;
            height: 74rpx;
            border-radius: 50%;
          }
        }

        .name {
          font-family: IPix;
          font-size: 22rpx;
          color: #000000;
        }
      }

      .user-id {
        font-family: IPix;
        font-size: 22rpx;
        color: #000000;
      }
    }

    .time-box {
      .time-icon {
        margin-right: 13rpx;

        image {
          width: 22rpx;
          height: 22rpx;
        }
      }

      .time-txt {
        font-family: IPix;
        font-size: 22rpx;
        color: #000000;
      }
    }

    .desc {
      margin: 18rpx 0;
      font-family: IPix;
      font-size: 24rpx;
      color: #000000;
      line-height: 36rpx;
    }

    .money-box {
      .money-left {
        font-family: IPix;
        font-size: 32rpx;
        color: #000000;
        font-weight: 900;
      }

      .money-right {
        image {
          width: 58rpx;
          height: 58rpx;
        }
      }
    }
  }
}
</style>